Generic Methods জেনেরিক্স ব্যবহার করে এমন মেথড যা নির্দিষ্ট টাইপের উপর নির্ভর না করে কাজ করতে পারে। অন্যদিকে, Exception Propagation হল প্রোগ্রামে ত্রুটি (Exception) ঘটলে কীভাবে তা এক লেয়ার থেকে অন্য লেয়ারে পাঠানো হয়।
Generic Methods
Generic Method এমন একটি মেথড যেখানে মেথডের সিগনেচারে জেনেরিক টাইপ প্যারামিটার ডিফাইন করা হয়। এই প্যারামিটার টাইপ মেথড কল করার সময় নির্ধারিত হয়।
Generic Method এর কাঠামো:
public <T> ReturnType methodName(T parameter) {
// method body
}
উদাহরণ: Generic Method ব্যবহার
public class GenericExample {
// A generic method that prints any type of array
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
String[] strArray = {"Java", "Generics", "Method"};
printArray(intArray); // Output: 1 2 3 4 5
printArray(strArray); // Output: Java Generics Method
}
}
Key Points:
printArrayমেথডটি যেকোনো টাইপের অ্যারে প্রিন্ট করতে পারে।<T>হল জেনেরিক টাইপ প্যারামিটার।
Bounded Type Parameters সহ Generic Methods
মাঝে মাঝে মেথডে ব্যবহৃত টাইপ প্যারামিটারকে সীমাবদ্ধ করা প্রয়োজন। এ ক্ষেত্রে, bounded type parameters ব্যবহার করা হয়।
public class BoundedExample {
// A generic method that calculates the sum of numbers
public static <T extends Number> double sumArray(T[] array) {
double sum = 0.0;
for (T element : array) {
sum += element.doubleValue();
}
return sum;
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
Double[] doubleArray = {1.1, 2.2, 3.3};
System.out.println("Sum of Integer array: " + sumArray(intArray)); // Output: 15.0
System.out.println("Sum of Double array: " + sumArray(doubleArray)); // Output: 6.6
}
}
ব্যাখ্যা:
- টাইপ প্যারামিটার
<T extends Number>দিয়ে নিশ্চিত করা হয় যে, শুধুমাত্রNumberক্লাসের সাবটাইপ ব্যবহার করা যাবে।
Generic Methods এবং Exception Propagation
Generic Method থেকে Checked Exception Propagation
Generic Methods এর সাথে Exception Propagation করতে হলে Exception Type কে জেনেরিক টাইপ হিসেবে ডিফাইন করা যেতে পারে।
import java.util.concurrent.Callable;
public class ExceptionPropagationExample {
// A generic method with exception propagation
public static <T> T execute(Callable<T> task) throws Exception {
return task.call();
}
public static void main(String[] args) {
try {
// Example of a successful execution
String result = execute(() -> "Hello, Generics!");
System.out.println(result);
// Example of a failing execution
execute(() -> {
throw new Exception("Something went wrong!");
});
} catch (Exception e) {
System.err.println("Caught Exception: " + e.getMessage());
}
}
}
ব্যাখ্যা:
executeমেথডটিCallable<T>ইন্টারফেস ব্যবহার করে জেনেরিক এক্সিকিউশন পরিচালনা করে।throws Exceptionব্যবহার করে Checked Exception এর Propagation নিশ্চিত করা হয়।
Generic Methods এবং Custom Exceptions
Custom Exceptions এর মাধ্যমে জেনেরিক মেথডে নির্দিষ্ট ধরনের ত্রুটি পরিচালনা করা যায়।
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class CustomExceptionExample {
public static <T> void validateNotNull(T value) throws CustomException {
if (value == null) {
throw new CustomException("Value cannot be null!");
}
System.out.println("Validated: " + value);
}
public static void main(String[] args) {
try {
validateNotNull("Java Generics"); // Valid input
validateNotNull(null); // Will throw CustomException
} catch (CustomException e) {
System.err.println("Caught Exception: " + e.getMessage());
}
}
}
Key Features:
- Custom Exception ব্যবহারের মাধ্যমে স্পষ্ট ত্রুটি পরিচালনা।
- জেনেরিক টাইপের জন্য ভ্যালিডেশন যুক্ত করা।
- Generic Methods কোডের পুনরায় ব্যবহারযোগ্যতা এবং টাইপ সেফটি বাড়ায়।
- Exception Propagation এর মাধ্যমে জেনেরিক মেথডে ত্রুটি পরিচালনা সহজ হয়।
- Custom Exceptions এবং Bounded Type Parameters জেনেরিক মেথডে আরও কার্যকর কাস্টমাইজেশন এবং টাইপ কন্ট্রোল প্রদান করে।
- প্রজেক্টের জটিলতার উপর নির্ভর করে Generic Methods এবং Exception Propagation ব্যবহার করা যায়।
Read more